<template>

	<view>
		<view class="jiangqie-searchbox">
			<!-- 搜索栏 -->
			<view class="jiangqie-search-input">
				<icon type="search" size="18" color="#999"></icon>
				<input confirm-type="search" placeholder="输入影视名称搜索影视" placeholder-class="jiangqie-input-plholder"
					v-model="searchVal" class="jiangqie-input" @input="handerSearch" />
				<text class="cuIcon-pullright text-blue" style="font-size: 20px;" @click="handerSearch"></text>
			</view>
		</view>

		<view>

			<view class="tab-content">
				<block>

					<!--banner end-->
					<view class="container">
						<scroll-view scroll-x class="bg-white nav text-center">
							<view class="cu-item" :class="item.id==TabCur?'text-blue cur':''"
								v-for="(item,index) in tabsList" :key="item.id" @tap="tabSelect(item)" :data-id="index">
								{{item.name}}
							</view>
						</scroll-view>
						<view class="list-box data-v-0034d6f4">
							<block v-for="(item,index) in postsLast" :key="index">
								<view class="" style="width: 33.33%;">
									<image :src="item.coverImg" mode="aspectFill" class="image data-v-0034d6f4"
										:index="index" :data-id="item.id"
										@click.stop.prevent="handlerArticleClick(item.id)">
									</image>
									<view class="item_name">
										{{item.name}}
									</view>
								</view>
							</block>
						</view>
					</view>
					<!-- 壁纸列表 end -->
					<!--加载loadding-->
					<tui-loadmore :visible="loaddingLast"></tui-loadmore>
					<tui-nomore :visible="!pullUpOnLast"></tui-nomore>
					<!--加载loadding-->
				</block>

				<view class="vie_Botm" v-show="isShow">
					<text class="line_botm"></text>
					<text class="botm_text">没有更多了</text>
					<text class="line_botm"></text>
				</view>

			</view>
		</view>
		<image src="@/static/images/huojian.png" class="goTop" v-if="floorstatus" @click="goTop"></image>

		<!--加载框 start-->
		<tui-loading :visible="loading"></tui-loading>
		<!--加载框 end-->
	</view>
</template>

<script>
	import tuiLoading from '../../components/loading/loading';
	import tuiLoadmore from '../../components/loadmore/loadmore';
	import tuiNomore from '../../components/nomore/nomore';
	import {
		indexMixin
	} from "./mixins/indexMixin.js"
	/*
	 * Author: 郭少
	 * github: https://github.com/guoshao99/wallpaper-applet
	 * gitee: https://gitee.com/guoshao99/wallpaper-applet
	 * QQ：2549789059
	 * 微信：2549789059
	 */

	const authentication = require('../../utils/authentication');

	const Constants = require('../../utils/constants');

	const http = require('../../utils/http.js');

	const platformEnv = require('../../utils/platformEnv.js');

	import advertUtil from '@/utils/curd/advertUtil.js'

	export default {
		components: {
			tuiLoading,
			tuiLoadmore,
			tuiNomore
		},
		mixins: [indexMixin],
		data() {
			return {
				pageNum: 1,

				//首页page
				cpageNum: 1,

				//分类page
				scrollTop: 0,

				//分类类型
				classType: 0,

				//当前首页滚动条
				cardCur: 0,

				background: '',
				floorstatus: false,
				banner: [],
				notice: [],
				currentTab: 0,

				//图片导航
				iconNav: [],

				//热门文章
				hot: [],

				//热门tab
				postsLast: [],

				loaddingLast: false,
				pullUpOnLast: true,

				//其他tab
				posts: [],

				loadding: false,
				pullUpOn: true,

				pagead: 1,
				pageadB: 0,
				pageadC: 0,
				thisPage: 'index',
				talents: [],
				canSearch: '',
				keyword: '',
				current: {},

				data: {
					link: '',
					jumpAppid: '',
					image: ''
				},

				setAD: {
					banner: '',
					videoid: '',
					gridid: '',
					customAid: '',
					customBid: '',
					customCid: ''
				},

				loading: false
			};
		},
		onLoad: function(options) {
			var that = this;
			// var params = {
			// 	url: '/platform/index',
			// 	method: 'GET',
			// 	data: {},
			// 	callBack: function(res) {
			// 		that.setData({
			// 			notice: res.data.notices,
			// 			banner: res.data.banners,
			// 			//iconNav: res.data.iconNav,
			// 			hot: res.data.hot,
			// 			talents: res.data.talents
			// 		});
			// 	}
			// };
			// http.request(params);
			that.init();

			// #ifdef MP
			advertUtil.advertInfo(this, () => {
				//加载插屏
				advertUtil.setInterstitialAd(this);
			});
			// #endif
		},
		onPullDownRefresh() {

			this.pageNum = 1; //加载文章
			this.PageCur = 1
			// this.initCategory(); //当处理完数据刷新后,停止当前页面的下拉刷新。
			this.postsLast = []
			this.getFilmList()
			uni.stopPullDownRefresh();
		},
		onReachBottom() {
			if (this.isShow) {
				return false
			}
			this.PageCur++;
			this.getFilmList()
		},
		onShow() {

			var that = this;

		},
		onHide() {
			advertUtil.clearAdInterval();
		},
		onShareAppMessage: function() {
			uni.showShareMenu({
				withShareTicket: true,
				menus: ['shareAppMessage', 'shareTimeline']
			});
			return {
				title: '免费下载高清壁纸,头像,表情包',
				path: '/pages/index/index',
			};
		},

		//转发朋友圈
		onShareTimeline: function() {
			var that = this;
			return {
				title: that.shares.share_title,
				imageUrl: that.shares.share_image
			};
		},
		onPageScroll: function(e) {
			// 获取滚动条当前位置
			if (this.currentTab == 0) {
				this.scrollTop = e.scrollTop;
			}

			if (e.scrollTop > 200) {
				this.setData({
					floorstatus: true
				});
			} else {
				this.setData({
					floorstatus: false
				});
			}
		},
		methods: {
			init: function() {
				//加载首页图片
				this.loadPostLast(true);
			},

			// 获取小程序插屏广告
			setInterstitialAd: function() {
				var that = this;
				console.logthat.setAD.interstitialid
				if (that.setAD.interstitialid && uni.createInterstitialAd) {
					let interstitialAd = uni.createInterstitialAd({
						adUnitId: that.setAD.interstitialid
					}); // 监听插屏错误事件

					interstitialAd.onError((err) => {
						console.error(err);
					}); // 显示广告

					if (interstitialAd) {
						if (that.setAD.switch_inad == 'yes') {
							setinad = setInterval(() => {
								interstitialAd.show().catch(err => {
									console.error(err);
								});
							}, 2000);
						} else {
							setTimeout(() => {
								interstitialAd.show().catch(err => {
									console.error(err);
								});
							}, 6000);
						}
					}
				}
			},

			//nav end ----
			//slide start----
			handlerSlideChange: function(e) {
				this.setData({
					current: e.detail.current
				});
			},

			//slide end----
			//tab -- start
			swichNav: function(e) {
				let cur = e.currentTarget.dataset.current;
				let classType = e.currentTarget.dataset.classtype;

				if (this.currentTab == cur) {
					return false;
				}

				this.setData({
					currentTab: cur,
					classType: classType,
					cpageNum: 1
				}); //分类数据

				this.loadPost(true); //如果点击首页进行定位

				uni.pageScrollTo({
					scrollTop: cur == 0 ? this.scrollTop : 0
				});
			},

			handlerTabMoreClick: function(e) {
				uni.switchTab({
					url: '/pages/categories/categories'
				});
			},

			//tab -- end
			handlerNoticeClick: function(e) {
				let link = e.currentTarget.dataset.link;
				let id = e.currentTarget.dataset.id;
				uni.navigateTo({
					url: link + '?id=' + id
				});
			},

			handlerIconNavClick: function(e) {
				let link = e.currentTarget.dataset.link;
				let jumpAppid = e.currentTarget.dataset.jumpappid;
				this.openLink(link, jumpAppid);
			},

			handlerActiveClick: function(e) {
				console.log(e)
				let link = e.currentTarget.dataset.link;
				let jumpAppid = e.currentTarget.dataset.jumpappid;
				this.openLink(link, jumpAppid);
			},

			handlerArticleClick(id) {
				uni.navigateTo({
					url: `/pages/article/article?id=${id}`,
				});
			},

			//加载数据
			loadPost: function(refresh) {
				let that = this;

				if (!refresh) {
					that.cpageNum++;
					that.setData({
						loadding: true
					});
				} //加载分类图片  begin

				var params = {
					url: '/platform/wallpaper',
					method: 'GET',
					data: {
						pageNum: that.cpageNum,
						pageSize: Constants.JQ_PER_PAGE_COUNT,
						order: -1,
						isSys: 1
					},
					callBack: function(res) {
						that.setData({
							loadding: false,
							posts: refresh ? res.data.list : that.posts.concat(res.data.list),
							pullUpOn: res.data.isLastPage ? false : res.data.isLastPage
						});

					}
				};
				http.request(params); // 加载分类 end
			},

			//加载数据
			loadPostLast: function(refresh) {
				let that = this;

				if (!refresh) {
					that.pageNum++;
					that.setData({
						loaddingLast: true
					});
				} //加载分类图片  begin

				var params = {
					url: '/platform/wallpaper',
					method: 'GET',
					data: {
						pageNum: that.pageNum,
						pageSize: Constants.JQ_PER_PAGE_COUNT,
						order: -1,
						isSys: 1
					},
					callBack: function(res) {
						that.setData({
							loaddingLast: false,
							postsLast: refresh ? res.data.list : that.postsLast.concat(res.data.list),
							pullUpOnLast: !res.data.isLastPage ? true : false
						});
					}
				};
				// http.request(params); // 加载分类 end
			},

			// cardSwiper
			cardSwiper(e) {
				this.setData({
					cardCur: e.detail.current
				});
			},

			openLink: function(link, jumpAppid) {
				if (jumpAppid == '' || jumpAppid == undefined) {
					if (link.startsWith('/pages')) {
						uni.navigateTo({
							url: link
						});
					}
				} else {
					if (link.startsWith('/pages')) {
						uni.navigateToMiniProgram({
							appId: jumpAppid,
							//appid
							path: link,
							//path
							extraData: {
								//参数
							},
							envVersion: 'release',

							//开发版develop 开发版 trial   体验版 release 正式版
							success(res) {
								console.log('跳转成功'); // 打开成功
							}
						});
					}
				}
			},

			goTop: function(e) {
				// 一键回到顶部
				if (uni.pageScrollTo) {
					uni.pageScrollTo({
						scrollTop: 0,
						duration: 500
					});
				} else {
					uni.showModal({
						title: '提示',
						content: '当前版本过低，无法使用该功能，请升级到最新版本后重试。'
					});
				}
			},

			talentClick: function(e) {
				let userid = e.currentTarget.dataset.userid;

				if (userid == 0) {
					uni.navigateTo({
						url: '/pages/talent/talent?userid=' + userid
					});
				} else {
					userid = userid.replace(/\s+/g, ''); //全局替换空格

					uni.navigateTo({
						url: '/pages/talent/mytalent/mytalent?userid=' + userid
					});
				}
			},
			kefu: function() {

				if (authentication.isLogin()) {
					uni.navigateTo({
						url: '/pages/apply/apply'
					});
				} else {
					uni.showModal({
						title: '提示',
						content: '创作者入住需要绑定信息，请先登录',
						success(res) {
							if (res.confirm) {
								uni.switchTab({
									url: '/pages/ucenter/ucenter'
								});
							}
						}
					})
				}

				// tt.openAwemeUserProfile({
				//   success: (res) => {
				//     console.log(res);
				//   },
				//   fail: function () {
				//       console.log('--- 转发失败 ---');
				//   }
				// });
			},
			getTalentInfo: function() {
				let that = this;

				uni.showLoading({
					title: '加载中' // 内容
				});
				var params = {
					url: '/talent/getTalentByWord',
					method: 'GET',
					data: {
						word: that.keyword
					},
					callBack: function(res) {
						uni.hideLoading();
						if (res.code == 200) {
							console.log(res.data);
							uni.navigateTo({
								url: '/pages/talent/mytalent/mytalent?userid=' + res.data.userid
							});
						} else {
							uni.navigateTo({
								url: '/pages/search/search?keyword=' + that.keyword
							});
						}

					}
				};
				http.request(params);
			},
		}
	};
</script>
<style>
	@import './index.css';

	.item_name {
		text-align: center;
		padding: 0 10px;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.vie_Botm {
		padding: 10px 0;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #ccc;
	}

	.line_botm {
		height: 1px;
		width: 15%;
		border-bottom: 1px solid #ccc;
	}

	.botm_text {
		margin: 0 10px;
	}
</style>